﻿using Edge.Repository.Model.CameraSys;
using Edge.Repository.Model.CameraSys.CDal;
using Edge.Repository.Model.EMS;
using HYRN.Common.ConvertUtilities;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CameraClient.CForm.CameraF
{
    public partial class FormCameraEdit : Form
    {

        CameraService dal = new CameraService();
        public int KeyId = 0;
        CameraConfigModel cameraOld = new CameraConfigModel();
        public FormCameraEdit(int id = 0)
        {
            InitializeComponent();
            KeyId = id;
            if (KeyId > 0)
            {
                CameraConfigModel camera = dal.QueryById(KeyId);
                if (camera != null)
                {
                    cameraOld = camera;
                    textBox1.Text = camera.Name;
                    textBox2.Text = camera.Source;
                    textBox3.Text = camera.BeiZhu;


                    numericUpDown1.Value = camera.MaxWenDu;
                    numericUpDown2.Value = camera.MinWenDu;
                    numericUpDown3.Value = cameraOld.Attribute1.ObjToDecimal();
                  


                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            // 保存


            int MaxWenDu = (int)numericUpDown1.Value;
            int MinWenDu = (int)numericUpDown2.Value;

            if (MaxWenDu <= MinWenDu) {
                MessageBox.Show("最大温度要大于最小温度！");
                return; }

            if (KeyId > 0)
            {
                cameraOld.Name = textBox1.Text;
                cameraOld.Source = textBox2.Text; ;
                cameraOld.BeiZhu = textBox3.Text;
                cameraOld.MaxWenDu = (int)numericUpDown1.Value;
                cameraOld.MinWenDu = (int)numericUpDown2.Value;
                cameraOld.Attribute1 =  numericUpDown3.Value.ToString();

                



                dal.Update(cameraOld);
            }
            else
            {
                CameraConfigModel cameraAdd = new CameraConfigModel();
                cameraAdd.Name = textBox1.Text;
                cameraAdd.Source = textBox2.Text;
                cameraAdd.BeiZhu = textBox3.Text;
                cameraAdd.MaxWenDu = (int)numericUpDown1.Value;
                cameraAdd.MinWenDu = (int)numericUpDown2.Value;
                cameraOld.Attribute1 = numericUpDown3.Value.ToString();
                dal.Add(cameraAdd);
            }

            MessageBox.Show("保存成功！");
            this.DialogResult = DialogResult.OK;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // 删除

            DialogResult dr = MessageBox.Show("确定要删除吗", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.Yes)
            {

                if (dal.Delete(dal.QueryById(KeyId)))
                {
                    MessageBox.Show("删除成功！");
                }
                else
                {
                    MessageBox.Show("删除失败！");
                }
            }
            this.DialogResult = DialogResult.OK;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            // 取消
            this.DialogResult = DialogResult.Cancel;
        }
    }
}
